﻿Public Class ProjectForm

    Private Sub ProjectsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectsBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.ProjectsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.ShopPartsDataSet)

    End Sub

    Private Sub ProjectForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'ShopPartsDataSet.Parts' table. You can move, or remove it, as needed.
        Me.PartsTableAdapter.Fill(Me.ShopPartsDataSet.Parts)
        'TODO: This line of code loads data into the 'ShopPartsDataSet.Projects' table. You can move, or remove it, as needed.
        Me.ProjectsTableAdapter.Fill(Me.ShopPartsDataSet.Projects)

    End Sub

    Private Sub ToolStripButtonExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButtonExcel.Click
        If MessageBox.Show("Create an Excel spread sheet with Data?", "Excel", MessageBoxButtons.YesNo) = System.Windows.Forms.DialogResult.Yes Then
            'verfying the datagridview having data or not
            If ((PartsDataGridView.Columns.Count = 0) Or (PartsDataGridView.Rows.Count = 0)) Then
                Exit Sub
            End If

            'Creating dataset to export
            Dim dset As New DataSet
            'add table to dataset
            dset.Tables.Add()
            'add column to that table
            For i As Integer = 0 To PartsDataGridView.ColumnCount - 1
                dset.Tables(0).Columns.Add(PartsDataGridView.Columns(i).HeaderText)
            Next
            'add rows to the table
            Dim dr1 As DataRow
            For i As Integer = 0 To PartsDataGridView.RowCount - 1
                dr1 = dset.Tables(0).NewRow
                For j As Integer = 0 To PartsDataGridView.Columns.Count - 1
                    dr1(j) = PartsDataGridView.Rows(i).Cells(j).Value
                Next
                dset.Tables(0).Rows.Add(dr1)
            Next

            Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
            Dim wBook As Microsoft.Office.Interop.Excel.Workbook
            Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

            wBook = excel.Workbooks.Add()
            wSheet = wBook.ActiveSheet()

            Dim dt As System.Data.DataTable = dset.Tables(0)
            Dim dc As System.Data.DataColumn
            Dim dr As System.Data.DataRow
            Dim colIndex As Integer = 0
            Dim rowIndex As Integer = 0

            For Each dc In dt.Columns
                colIndex = colIndex + 1
                excel.Cells(1, colIndex) = dc.ColumnName
            Next

            For Each dr In dt.Rows
                rowIndex = rowIndex + 1
                colIndex = 0
                For Each dc In dt.Columns
                    colIndex = colIndex + 1
                    excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)

                Next
            Next

            wSheet.Columns.AutoFit()
            excel.Visible = True
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excel)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wBook)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wSheet)
            excel = Nothing
        End If
    End Sub

    Private Sub LocationTextBox_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LocationTextBox.DoubleClick
        Dim ProjectPath As String
        If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
            ProjectPath = FolderBrowserDialog1.SelectedPath
            LocationTextBox.Text = ProjectPath
        End If
    End Sub
End Class